using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Controls
{
    public class BaseControl : System.Web.UI.UserControl
    {
        private EventHandler UserLoggedInHandler;
        private EventHandler UserLoggedOutHandler;

        public BaseControl()
        {
            UserLoggedInHandler += new EventHandler(UserLoggedIn);
            UserLoggedOutHandler += new EventHandler(UserLoggedOut);
            Managers.UserLoginManager.Instance.OnUserLoggedIn += UserLoggedInHandler;
            Managers.UserLoginManager.Instance.OnUserLoggedOut += UserLoggedOutHandler;
        }

        protected virtual void UserLoggedIn(object sender, EventArgs e)
        {}

        protected virtual void UserLoggedOut(object sender, EventArgs e)
        {}

        public override void Dispose()
        {
            Managers.UserLoginManager.Instance.OnUserLoggedIn -= UserLoggedInHandler;
            Managers.UserLoginManager.Instance.OnUserLoggedOut -= UserLoggedOutHandler;
            base.Dispose();
        }
    }

    public abstract class BaseDataManagerControl<T> : BaseControl
        where T : DAL.DataManagerRow
    {

        private T _currentItem;
        protected T CurrentItem
        {
            get
            {
                if (ViewState["CurrentItemID"] != null)
                {
                    long itemID = 0;
                    if (Int64.TryParse(ViewState["CurrentItemID"].ToString(), out itemID))
                    {
                        if (_currentItem == null || _currentItem.ID != itemID)
                        {
                            _currentItem = GetItem(itemID);
                        }
                        return _currentItem;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (value != null)
                {
                    ViewState["CurrentItemID"] = value.ID;
                }
                else
                {
                    ViewState["CurrentItemID"] = null;
                }
                ItemChanged();
            }
        }

        public virtual string QueryStringName
        {
            get
            {
                return "";
            }
        }

        protected T GetItemFromQueryString()
        {
            if (String.IsNullOrEmpty(QueryStringName))
            {
                return null;
            }
            else
            {
                string queryStringValue = Request.QueryString[QueryStringName];
                if (String.IsNullOrEmpty(queryStringValue))
                {
                    return null;
                }
                else
                {
                    long itemID;
                    if (Int64.TryParse(queryStringValue, out itemID))
                    {
                        return GetItem(itemID);
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        protected abstract T GetItem(long id);
        protected abstract void ItemChanged();
    }
}